/*
 * Copyright (c) 2023, LXMS (lxinet.cn).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.lxinet.lxms.framework.mybatis.helper;

import cn.lxinet.lxms.framework.mybatis.annotation.Id;
import cn.lxinet.lxms.framework.mybatis.annotation.LogicDel;
import cn.lxinet.lxms.framework.mybatis.annotation.Table;
import cn.lxinet.lxms.framework.mybatis.vo.TableInfo;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class TableHelper {

    public static final Map<Class<?>, TableInfo> TABLE_CACHE = new HashMap<>();

    public static void initTable(Class<?> c){
        TableInfo tableInfo = new TableInfo();
        tableInfo.setClazz(c);
        setTableName(c, tableInfo);
        setIdColumn(c, tableInfo);
        setLogicDel(c, tableInfo);
        TABLE_CACHE.put(c, tableInfo);
    }

    /**
     * 获取ID字段
     *
     * @param c c
     * @return {@link String}
     */
    public static void setIdColumn(Class<?> c, TableInfo tableInfo) {
        Field[] fields = c.getDeclaredFields();
        for (Field field : fields) {
            Id annotation = field.getAnnotation(Id.class);
            if (null != annotation) {
                tableInfo.setIdColumn(field.getName());
                break;
            }
        }
    }


    /**
     * 获得逻辑删除字段
     *
     * @param c c
     * @return {@link String}
     */
    public static void setLogicDel(Class<?> c, TableInfo tableInfo) {
        Field[] fields = c.getDeclaredFields();
        for (Field field : fields) {
            LogicDel annotation = field.getAnnotation(LogicDel.class);
            if (null != annotation) {
                tableInfo.setLogicDelValue(annotation.logicDelValue());
                tableInfo.setLogicNoDelValue(annotation.logicNoDelValue());
                tableInfo.setLogicDelColumn(field.getName());
                break;
            }
        }
    }



    /**
     * 获取表名
     *
     * @param c c
     * @return {@link String}
     */
    public static void setTableName(Class<?> c, TableInfo tableInfo) {
        Table annotation = c.getAnnotation(Table.class);
        tableInfo.setTableName(annotation.value());
    }


    /**
     * 获取表名
     *
     * @param c c
     * @return {@link String}
     */
    public static String getTableName(Class<?> c) {
        if (TABLE_CACHE.get(c) == null){
            initTable(c);
        }
        return TABLE_CACHE.get(c).getTableName();
    }

    /**
     * 获取ID字段
     *
     * @param c c
     * @return {@link String}
     */
    public static String getIdName(Class<?> c) {
        if (TABLE_CACHE.get(c) == null){
            initTable(c);
        }
        return TABLE_CACHE.get(c).getIdColumn();
    }

    /**
     * 获得逻辑删除字段
     *
     * @param c c
     * @return {@link String}
     */
    public static String getLogicDel(Class<?> c) {
        if (TABLE_CACHE.get(c) == null){
            initTable(c);
        }
        return TABLE_CACHE.get(c).getLogicDelColumn();
    }

    /**
     * 获得逻辑删除的值
     *
     * @param c c
     * @return {@link String}
     */
    public static int getLogicDelValue(Class<?> c) {
        if (TABLE_CACHE.get(c) == null){
            initTable(c);
        }
        return TABLE_CACHE.get(c).getLogicDelValue();
    }
    /**
     * 获得逻辑未删除的值
     *
     * @param c c
     * @return {@link String}
     */
    public static int getLogicNoDelValue(Class<?> c) {
        if (TABLE_CACHE.get(c) == null){
            initTable(c);
        }
        return TABLE_CACHE.get(c).getLogicNoDelValue();
    }
}
